<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="plug/mui/css/app.css">
    <link rel="stylesheet" href="./plug/mui/css/mui.min.css">
    <link rel="stylesheet" href="plug/mui/css/mui.picker.min.css">
    <link rel="stylesheet" href="./css/login.css">
    <script>
      let userName = localStorage.getItem('userName') ,
          token= localStorage.getItem('token' );
      console.log(token,userName)
        if (userName && token){
            window.location.href ='index.html'
        }
    </script>
</head>
<body>
    <div class="login-box">
        <div class="set">
            <img src="./img/set.png" alt="设置">
        </div>
        <div class="login-from">
            <form>
                <div class="input-box">
                    <input type="text" placeholder="用户名" autocomplete="off" id="user">
                    <img src="./img/user.png" alt="">
                </div>
                <div class="input-box">
                    <input type="password" placeholder="密码" id="password">
                    <img src="./img/password.png" alt="">
                </div>
                <div class="checkbox">
                    <input type="checkbox" id="checkbox">
                    <label for="checkbox">记住密码</label>
                </div>
            </form>
            <button type="submit">登录</button>
        </div>
        <div class="bottom">
            <img src="./img/img.png" alt="">
            <div>
                由河南卫特机器人有限公司提供技术支持
                <span></span>
                www.cranewh.com
                <span></span>
                总部地址：河南自贸试验区郑州片区（郑东）金水东路49号
                <span></span>
                电话：400-005-8886
            </div>
        </div>
    </div>
</body>
<script src="plug/jquery.min.js"></script>
<script src="plug/mui/js/mui.min.js"></script>
<script src="plug/mui/js/mui.picker.min.js"></script>
<script>
    $(document).ready(function () {
        let  ipValue = '' //ip
        ipValue = localStorage.getItem('ipValue') || "127.0.0.1";
        if (!ipValue) {
            mui.toast('请先设置ip', {
                duration: 'long',
                type: 'div'
            })
        } else {
        }
        $('.set').click(function (){
            mui.prompt('ip', '请输入ip', '提示', ['取消', '确定'], function (e) {
                if (e.index == 1) {
                    let val = e.value.trim()
                    let flag = isValidIP(val)
                    if (!flag) {
                        mui.toast('请输入正确的IP地址')
                        return false
                    }
                    ipValue = val
                    localStorage.setItem('ipValue', val);
                }
            }, 'div')
            //输入框失去焦点
            document.querySelector('.mui-popup-input').firstChild.blur()
            //默认值设置
            document.querySelector('.mui-popup-input').firstChild.value = ipValue
        })
        $('.login-from button').click(function (){
            let user = '',
                password = ''
            user = $('.login-from #user').val()
            password =$(' #password').val()
            if (!user){
                mui.toast('请输入账号')
                return false
            }
            if (!password){
                mui.toast('请输入密码')
                return false
            }
            $.ajax({
                url: `http://${ipValue}:55200/v1/pad/login?user_code=${user}&user_pwd=${password}`,
                contentType: "multipart/form-data",
                data: '',
                type: "post",
                dataType: "json",
				// xhrFields: {
				// 	withCredentials: true
				// },
                success: (res) => {
                    if (res.code == 200) {
                        let {userName,token} = {...res }
                        localStorage.setItem('userName', userName);
                        localStorage.setItem('token', token);
                        window.location.href ='index.html'
                    }else {
                        mui.toast(res.msg)
                    }
                },
				error:function () {
				    mui.toast('请检查IP地址是否正确的')
				}
            })
        })
        function isValidIP(ip) {
            var reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
            return reg.test(ip);
        }
    })
</script>
</html>
